package com.dongdongshop.config;



import com.dongdongshop.pojo.TbUser;

import com.dongdongshop.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;


public class LoginRealm<LzMenuService> extends AuthorizingRealm {

    @DubboReference
    private UserService tbUserService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        System.out.println("授权方法");
//        获取当前登录的用户
//        LzPerson person = (LzPerson) SecurityUtils.getSubject().getPrincipal();
//        //根据id查出来此id所拥有的权限
//        List<String> list = ms.getPermsById(person.getPid());
//        将这些权限添加
//        simpleAuthorizationInfo.addStringPermissions(list);
        return simpleAuthorizationInfo;
    }


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认证方法");
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        TbUser user = tbUserService.getUserByPname(token.getUsername());
        if(user == null){
            return null;
        }
        return new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getSalt()), user.getUsername());
    }

}
